package com.shux.utils.object;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 **********************************************
 *  描述：Object相关工具类
 * Simba.Hua
 * 2017年5月22日
 **********************************************
**/
public class ObjectUtil {
	/**
	 * 通过Object获取int
	 */
	public static int getInt(Object o){
		return Integer.parseInt(String.valueOf(o));
	}
	
	public static Object toObject(byte[] valueb) {
      ByteArrayInputStream ais = new ByteArrayInputStream(valueb);
      Object value = null;
      try {
          ObjectInputStream oos = new ObjectInputStream(ais);
          value = oos.readObject();
          ais.close();
          oos.close();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return value;
  }
  
  /**
   * 对象转换成byte[]数组
   * @param valuef
   * @return
   */
	public static byte[] toByteArray(Object valuef) {
      byte[] bytes = null;
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      try {
          ObjectOutputStream oos = new ObjectOutputStream(outputStream);
          oos.writeObject(valuef);
          oos.flush();
          bytes = outputStream.toByteArray();
          oos.close();
          outputStream.close();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return bytes;
  }
}
